<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>首页</title>
    <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <script src="__PUBLIC__/js/jquery-1.9.0.js"></script>
    <script src="__PUBLIC__/home/plugins/layer/layer.js"></script>
    <script src="__PUBLIC__/js/angular.js"></script>
    <script src="__PUBLIC__/common.js"></script>
    <!--标准mui.css-->
    <link rel="stylesheet" href="__PUBLIC__/mui/css/mui.css">
    <!--App自定义的css-->
    <link rel="stylesheet" href="__PUBLIC__/css/app.css">
    <link rel="stylesheet" href="__PUBLIC__/css/cart.css">
    <script src="__PUBLIC__/js/jquery.qrcode.min.js"></script>
    <style>
        .enter {
            background: url("__PUBLIC__/images/enter-right.png") no-repeat right center;
            background-size: auto 50%;
        }

        .cart .cart-goods {
            margin-bottom: 0;
        }

        .address {
            margin-top: 15px;
        }
    </style>

</head>
<body ng-app="myApp" ng-controller="cartCtrl" ng-cloak>

<!--header-->
<header class="mui-bar mui-bar-nav" >
    <a class="mui-icon mui-icon-left-nav mui-pull-left" style="color: white" href="/index.php/Home/cart/cart"></a>
    <h1 class="mui-title">确认订单</h1>
</header>

<!--购物车-->
<div class="mui-content cart">
    <ul class="mui-table-view" ng-repeat="pro in cart">
        <li class="mui-table-view-cell mui-media cart-goods">
            <a href="javascript:;">

                <img class="media-object mui-pull-left" ng-src="{{pro.img}}">
                <div class="mui-media-body">
                    <span>{{pro.name}}</span>
                    <p class='mui-ellipsis'>
                    <div class=" mui-pull-right" data-numbox-min='0' style="margin-right: 10%">
                        <span ng-hide="pro.num*1 >= pro.boxnum*1">{{pro.num}}盒</span>
                        <span ng-show="pro.num*1 >= pro.boxnum*1">{{floor(pro.num/pro.boxnum)}}箱{{pro.num%pro.boxnum}}盒</span>
                    </div>
                    </p>
                    <p class='mui-ellipsis'>{{pro.introduce}}</p>
                    <p class='mui-ellipsis money'>￥<b>{{pro.price}}</b></p>
                </div>
            </a>
        </li>
    </ul>
    <!--暂时去掉-->
    <ul class="mui-table-view address">
        <li class="mui-table-view-cell mui-media" style="padding: 4px;text-indent: 1em;background: lightgray">收货地址</li>
        <li class="mui-table-view-cell mui-media cart-goods">
            <div class="mui-media-body" style="width: 80%;float: left">
                <span style="width: 90%">{{memberAddress.name_path}} {{memberAddress.address}}</span>
                <span style="width: 90%">{{memberAddress.name}}<label
                        style="margin-left: 3em">{{memberAddress.phone}}</label></span>
            </div>
            <div style="width: 20%;height:42px;float: left" class="enter">
                <a href="/index.php/home/member/address?flg=2" style="display: block;width: 100%;height: 100%"></a>
            </div>
        </li>
    </ul>

</div>
<div style="height: 100px">
</div>
<!--结算&删除-->
<div class="cart-total " style="bottom: 0;">
    <ul class="mui-table-view">
        <li class=" mui-media ">

            <div class="mui-media-body  do-cart" style="display: flex;margin-left: 40px">
                <p class='money-all'>实付款:￥<span id="money">{{total}}</span></p>
                <p class='go-deal' id="go-deal" style="overflow:visible" ng-click="confirmNow()">立即支付</p>
            </div>

        </li>
    </ul>

</div>
<script>
    var app = getApp("myApp", []);

    app.controller('cartCtrl', function ($scope, $http) {
        $http({
            method: 'GET',
            url: "/index.php/Home/Cart/getDoCartInfo"
        }).then(function successCallback(response) {
            //console.log(response.data.data);
            if(response.data.code == -1){
                layer.msg(response.data.msg, {time: 1000})
                setTimeout(function () {
                    window.location.href="/index.php/Home/index/index"
                }, 1000)
            }else{
                // console.log(response.data.data);
                $scope.cart = response.data.data.cart;
                $scope.member = response.data.data.member;
                $scope.memberAddress = response.data.data.memberAddress;
                $scope.ftotal = response.data.data.total;
                $scope.total = response.data.data.total;
            }
        }, function errorCallback(response) {

        });
        $scope.useavlb = false;

        $scope.confirmNow = function () {
            layer.confirm('是否要支付？', {
                btn: [ '确认','取消'] //按钮
            }, function () {
                var data = new Array();
                var i = 0;
                for (var c in $scope.cart) {
                    if ($scope.cart[c].num > 0) {
                        data[i] = $scope.cart[c].id;
                        data[i + 1] = $scope.cart[c].num;
                        i = i + 2;
                    }
                }
                if(i > 0) {
                    $http({
                        method: 'POST',
                        data: {
                            'addressId': $scope.memberAddress.id,
                            'checked': $scope.useavlb,
                            'products':data
                        },
                        headers: {'Content-Type': 'application/x-www-form-urlencoded'},
                        transformRequest: function (obj) {
                            var str = [];
                            for (var p in obj) {
                                str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
                            }
                            return str.join("&");
                        },
                        url: "/index.php/Home/OnlineOrder/formatCartData"
                    }).then(function successCallback(response) {
                        //console.log(response.data);
                        successHandle(response, function () {
                            // console.log(response.data);
                            if (-1 == response.data.code) {
                                layer.msg(response.data.message);
                            } else if (0 == response.data.code) {
                                layer.msg("提交订单成功！");
                                setTimeout(function () {
                                    window.location.href = "/Home/Member/mylist";
                                }, 1000)

                            }
                            //layer.msg(response.data.data);
                            // var orderid = response.data.data.order.id
                            //微信-Jsapi支付；非微信-二维码支付
                            // if($scope.isWeiXin()){
                            //     console.log('wx');
                            //     $scope.getJsapiPay(orderid);
                            // } else {
                            //     console.log('notwx');
                            //     $scope.getQrPay(orderid);
                            // }
                        });
                    }, function errorCallback(response) {
                        errorHandle(response);
                    });
                }
            }, function () {
                layer.msg('已取消');
            })
        }



        //立即支付
        // $scope.confirmNow = function () {
        //     layer.confirm('是否要支付？', {
        //         btn: [ '确认','取消'] //按钮
        //     }, function () {
        //         var data = new Array();
        //         var i = 0;
        //         for (var c in $scope.cart) {
        //             if ($scope.cart[c].num > 0) {
        //                 data[i] = $scope.cart[c].id;
        //                 data[i + 1] = $scope.cart[c].num;
        //                 i = i + 2;
        //             }
        //         }
        //         if(i > 0) {
        //             $http({
        //                 method: 'POST',
        //                 data: {
        //                     'addressId': $scope.memberAddress.id,
        //                     'checked': $scope.useavlb,
        //                     'products':data
        //                 },
        //                 headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        //                 transformRequest: function (obj) {
        //                     var str = [];
        //                     for (var p in obj) {
        //                         str.push(encodeURIComponent(p) + "=" + encodeURIComponent(obj[p]));
        //                     }
        //                     return str.join("&");
        //                 },
        //                 url: "/index.php/Home/OnlineOrder/createOrder"
        //             }).then(function successCallback(response) {
        //                 //console.log(response.data);
        //                 successHandle(response, function () {
        //                     //layer.msg(response.data.data);
        //                     var orderid = response.data.data.order.id
        //                     //微信-Jsapi支付；非微信-二维码支付
        //                     if($scope.isWeiXin()){
        //                         console.log('wx');
        //                         $scope.getJsapiPay(orderid);
        //                     } else {
        //                         console.log('notwx');
        //                         $scope.getQrPay(orderid);
        //                     }
        //                 });
        //             }, function errorCallback(response) {
        //                 errorHandle(response);
        //             });
        //         }
        //     }, function () {
        //         layer.msg('已取消');
        //     })
        // }

        //接入微信扫码支付
        // $scope.getQrPay = function (orderid) {
        //     //用ajax获取到支付的二维码
        //     var payQrcode = '';
        //     var payPrice = '';
        //     var reqsn = '';
        //     $.post('/index.php/Home/PayWx/getQrcode',{"orderid":orderid},function(result) {
        //         var obj = result.data;
        //         //console.log(obj);
        //         if (obj.status == 'success') {
        //             payQrcode = obj.code;
        //             payPrice = obj.price;
        //             reqsn = obj.sn;
        //             //console.log(payQrcode);
        //             layer.open({
        //                 type: 1,
        //                 skin: 'layui-layer-demo', //样式类名
        //                 closeBtn: 0, //不显示关闭按钮
        //                 title: false,
        //                 anim: 2,
        //                 area: ['300px', '450px'],
        //                 shadeClose: true, //开启遮罩关闭
        //                 content: '<div style="color: #666;margin: 5px 0 0 10px;position: absolute;right:.5rem;" onclick="layer.closeAll()">X</div>' + '<div style="text-align: center;"><img style="width: 4rem;padding:2rem 0 1rem 0;" src="__PUBLIC__/images/wx.png" alt="" /><br><b style="font-weight: 100;color: #888;">请打开微信 扫码进行支付 </b><div class="blank"></div>支付金额：<span style="color: red;">' + payPrice + '</span> 元</div><div class="blank"></div>' + '<div style="text-align: center"><div id="code" style="margin-left: 5px;margin-top: 10px"></div></div><div id="myDiv" style="width: 100%;height: 1.5rem;bottom: 2rem;position:absolute;text-align:center;font-size: 1.2rem;color: #04BE02;"></div><div id="timer" style="display:none;">0</div>'
        //             });
        //             $("#code").qrcode({
        //                 render: "canvas",
        //                 width: 220, //宽度
        //                 height: 220, //高度
        //                 text: payQrcode //任意内容
        //             });
        //
        //             // 设置每隔3000毫秒执行一次查询方法
        //             var myIntval = setInterval(function () {
        //                 orderQuery(reqsn)
        //             }, 3001);
        //             //查询订单状态
        //             function orderQuery(reqsn) {
        //                 //layer.closeAll();
        //                 $.ajax({
        //                     url: "{:U('PayWx/orderQuery')}",
        //                     type: "post",
        //                     dataType: "json",
        //                     data: {
        //                         reqsn: reqsn
        //                     },
        //                     timeout: 5000,
        //                     success: function (result) {
        //                         var obj = result.data;
        //                         if (obj.result_code == "SUCCESS") {
        //                             var status = obj.trade_state;
        //                             var statusdesc = obj.trade_state_desc;
        //                             //console.log(status);
        //                             if (status == 'SUCCESS') {
        //                                 document.getElementById("myDiv").innerHTML = '支付完成';
        //                                 clearInterval(myIntval);
        //                                 console.log("支付成功");
        //                                 setTimeout(function () {
        //                                     window.location.href = "/index.php/Home/Member/mylist"
        //                                 },1000)
        //                             } else if (status == 'USERPAYING') {
        //                                 document.getElementById("myDiv").innerHTML = '正在支付';
        //                                 clearInterval(myIntval);
        //                             } else if (status == 'CLOSED') {
        //                                 document.getElementById("myDiv").innerHTML = '交易已关闭';
        //                                 clearInterval(myIntval);
        //                             } else if (status == 'PAYERROR') {
        //                                 document.getElementById("myDiv").innerHTML = '交易失败';
        //                                 clearInterval(myIntval);
        //                             } else if (status == 'NOTPAY') {
        //                                 document.getElementById("myDiv").innerHTML = '交易处理中';
        //                             } else if (status == 'REVOKED') {
        //                                 document.getElementById("myDiv").innerHTML = '交易已撤销';
        //                                 clearInterval(myIntval);
        //                             }
        //                         }
        //                     },
        //                     error: function (result) {
        //                     }
        //                 })
        //             }
        //         }
        //     })
        // }

        //接入微信JSAPI支付
        // $scope.getJsapiPay = function (orderid) {
        //     //用ajax获取到支付信息
        //     $.post('/index.php/Home/PayWx/getJsapi', {"orderid": orderid}, function (ret) {
        //         // 如果有错误
        //         if(ret.code != 0){
        //             layer.msg(ret.msg);
        //             return;
        //         }
        //
        //         $scope.jsapipara = JSON.parse(ret.data);
        //         //console.log($scope.jsapipara.appId)
        //         $scope.callWxpay(orderid);
        //     });
        // }

        //判断是不是微信
        // $scope.isWeiXin = function(){
        //     var ua = window.navigator.userAgent.toLowerCase();
        //     if(ua.match(/MicroMessenger/i) == 'micromessenger'){
        //         return true;
        //     }else{
        //         return false;
        //     }
        // }

        //发起微信JSapi支付并接受回调
        // $scope.jsApiCall = function(orderid){
        //     WeixinJSBridge.invoke('getBrandWCPayRequest', {
        //                 "appId":$scope.jsapipara.appId,     //公众号名称，由商户传入
        //                 "timeStamp":$scope.jsapipara.timeStamp,         //时间戳，自1970年以来的秒数
        //                 "nonceStr":$scope.jsapipara.nonceStr, //随机串
        //                 "package":$scope.jsapipara.package,
        //                 "signType":$scope.jsapipara.signType,         //微信签名方式：
        //                 "paySign":$scope.jsapipara.paySign //微信签名
        //             },
        //             function(res){
        //                 WeixinJSBridge.log(res.err_msg);
        //                 //支付回调
        //                 if(res.err_msg == "get_brand_wcpay_request:ok"){
        //                     console.log("支付成功");
        //                     //$scope.paySuccess(orderid);
        //                     setTimeout(function () {
        //                         window.location.href = "/index.php/Home/Member/mylist"
        //                     },1000)
        //                 }else if(res.err_msg == "get_brand_wcpay_request:cancel"){
        //                     layer.msg("用户取消支付!");
        //                 }else{
        //                     console.log(res);
        //                     layer.msg("支付失败!");
        //                 }
        //                 //alert(res.err_code+res.err_desc+res.err_msg);
        //             }
        //     );
        // }

        //调用微信JSapi支付
        // $scope.callWxpay = function(orderid){
        //     if (typeof WeixinJSBridge == "undefined"){
        //         if( document.addEventListener ){
        //             document.addEventListener('WeixinJSBridgeReady', $scope.jsApiCall, false);
        //         }else if (document.attachEvent){
        //             document.attachEvent('WeixinJSBridgeReady', $scope.jsApiCall);
        //             document.attachEvent('onWeixinJSBridgeReady', $scope.jsApiCall);
        //         }
        //     }else{
        //         $scope.jsApiCall(orderid);
        //     }
        // }

        //积分抵扣计算
        // $scope.jisuan = function () {
        //     if($scope.useavlb == true){
        //         $scope.total = $scope.ftotal - $scope.avlbIntegral;
        //     } else {
        //         $scope.total = $scope.ftotal;
        //     }
        // }
    })
</script>

</body>
<script src="__PUBLIC__/mui/js/mui.min.js"></script>
</html>